﻿using System;
using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using UnityEngine;
using UnityEngine.EventSystems;

// ReSharper disable once CheckNamespace
public class ClickManager : SingletonMono<ClickManager>
{
    private Dictionary<Vector2Int, Lattice> chessboardDict = new();

    public Lattice currentLattice;
    private bool isSpotlighting;

    public bool gameStarted = false;
    

    public void StartGame()
    {
        chessboardDict = ChessBoardMgr.GetInstance().ChessboardDict;
        gameStarted = true;
    }


    private void Update()
    {
        if(gameStarted == false) return;
        if (Input.GetMouseButtonDown(0))
        {
            // 检查是否点击到 UI 元素
            if (EventSystem.current.IsPointerOverGameObject())
            {
                return;
            }
            // ReSharper disable once Unity.PerformanceCriticalCodeInvocation
            IsClickedObject();
        }
    }

    
    private void IsClickedObject()
    {
        
        // 射线检测，查看射线与哪些对象相交
        
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
        if (hit.collider)
        {
            // 如果射线与对象相交，检查这个对象是否是当前玩家控制的对象
            Lattice clickLattice = hit.collider.gameObject.GetComponent<Lattice>();

            if (!clickLattice)
            {
                //点击的格子没有Lattice脚本,点击的不是格子
                //焦点置为空   清空焦点,更新UI
                ClearSpotlight();
                print("点击的是没有Lattice脚本的物体");
            }
            else
            {
                //点击的物体都有Lattice脚本
                if (clickLattice.canClick)
                {
                    //点击的是目标格子
                    EventCenter.GetInstance().EventTrigger("ClickTargetLattice",clickLattice);
                    currentLattice = null;
                    print("点击的是目标格子,执行逻辑,焦点置为空");
                }
                else
                {
                    //点击的不是目标格子,但 是格子,
                    if (clickLattice.chessDate)
                    {
                        if (currentLattice && clickLattice.chessDate == currentLattice.chessDate)
                        {
                            //点击的是自己且自己不为空
                            EventCenter.GetInstance().EventTrigger("ClickSelf",clickLattice);
                            print("点击的是自己");
                        }
                        else
                        {
                            //格子里有棋子数据且这个数据于现在的焦点不同,更换焦点,更新UI
                            currentLattice = clickLattice;
                            EventCenter.GetInstance().EventTrigger("SpotlightChanged",clickLattice);
                            print("点击的是其他有棋子的格子,且这个棋子不是目标更换焦点");
                        }
                        
                    }
                    else
                    {
                        //格子中没有棋子数据    清空焦点,更新UI
                        ClearSpotlight();
                        print("点击的是空的Lattice且这个Lattice不是目标");
                    }
                }
            }
        }
        else
        {
            //点击物体没有碰撞器,  清空焦点,更新UI
            ClearSpotlight();
            print("点击的东西没有碰撞器,焦点置为空");
        }


    }

    private void ClearSpotlight()
    {
        //清空焦点,更新UI
        currentLattice = null;
        EventCenter.GetInstance().EventTrigger("SpotlightClear");
    }

    public IEnumerator ReallyMoveChess(Lattice lattice)
    {
        yield return 1;
        currentLattice  = lattice;
    }

    public void MoveChess(Lattice lattice)
    {
        StartCoroutine(ReallyMoveChess(lattice));
    }
}

